package com.tour.util;


import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Util {
    /**
     * 将字符串时间转为时间类
     * @param sdate 2020-03-13 00:00:00
     * @param fmt yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static Date getDateByString(String sdate,String fmt) throws ParseException {
        if(StringUtils.isEmpty(fmt)){
            fmt="yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(fmt);
        Date date = sdf.parse(sdate);
        return date;
    }

    /**
     * 获取两个时间的时间差
     * @return
     */
    public static String getDateDiffer(String beginDate,String endDate) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date now = df.parse(endDate);
        java.util.Date date=df.parse(beginDate);
        long l=now.getTime()-date.getTime();
        long day=l/(24*60*60*1000);
        long hour=(l/(60*60*1000)-day*24);
        long min=((l/(60*1000))-day*24*60-hour*60);
        long s=(l/1000-day*24*60*60-hour*60*60-min*60);
        String result=""+day+"天"+hour+"小时"+min+"分"+s+"秒";
        return result;
    }

    public static void main(String[] args) throws Exception{
        getDateDiffer("","");
    }
}
